package com.kla.power.controller;

import com.kla.power.config.LogPointer;
import com.kla.power.entity.MenuEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IMenuService;
import com.kla.power.service.IUserService;
import com.kla.power.util.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2024-06-02
 */
@RestController
@RequestMapping ( "/user" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class UserController {

    @Resource
    private IUserService iUserService;
    @Resource
    private IMenuService iMenuService;


    @RequestMapping ( "hello" )
    public String hello () {

        return "hello";
    }


    @RequestMapping ( "queryList" )
    @LogPointer ( "查询用户列表" )
    public ResultBody queryList ( @RequestBody UserEntity userEntity ) {
        UserEntity user = (UserEntity) RedisUtils.get ( userEntity.getToken () );
        userEntity.setMerchantId ( user.getMerchantId () );
        List<UserEntity> userEntities = iUserService.queryList ( userEntity );
        return ResultBody.success ( userEntities, iUserService.queryCount ( userEntity ) );
    }


    @RequestMapping ( "querySingle" )
    @LogPointer ( "登录" )
    public ResultBody querySingle ( @RequestBody UserEntity userEntity ) {
        userEntity = iUserService.querySingle ( userEntity );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.RESULT_NULL_EXCE );
        }
        // 修改最后登录时间
        userEntity.setLastLoginTime ( new Date () );
        iUserService.updateSingle ( userEntity );
        String token = CommonUtil.getToken ();
        RedisUtils.set ( token, userEntity, 2592000L, TimeUnit.SECONDS );
        userEntity.setToken ( token );
        // 先获取该用户的一级菜单权限
        MenuEntity menuEntity = new MenuEntity ();
        menuEntity.setType ( ConstUtil.MENU_TYPE_PARENT );
        menuEntity.setRoleId ( userEntity.getRoleId () );
        List<MenuEntity> menuEntities = iMenuService.queryMenus ( menuEntity );
        userEntity.setMenuEntities ( menuEntities );
        // 获取用户的所有三级按钮权限，绑定到缓存中
        MenuEntity btnMenu = new MenuEntity ();
        btnMenu.setType ( ConstUtil.MENU_TYPE_BTN );
        btnMenu.setRoleId ( userEntity.getRoleId () );
        List<MenuEntity> btnMenuEntities = iMenuService.queryList ( menuEntity );
        RedisUtils.set ( userEntity.getId (), btnMenuEntities, 2592000L, TimeUnit.SECONDS );
        return ResultBody.success ( userEntity );
    }


    @RequestMapping ( "saveSingle" )
    @LogPointer ( "保存用户" )
    public ResultBody saveSingle ( @RequestBody UserEntity userEntity ) {
        userEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        userEntity = iUserService.saveSingle ( userEntity );
        return ResultBody.success ( userEntity );
    }


    /**
     * 当前登录用户信息
     *
     * @param userEntity
     * @return
     */
    @RequestMapping ( "currentUser" )
    @LogPointer ( "获取当前登录用户" )
    public ResultBody currentUser ( @RequestBody UserEntity userEntity ) {
        UserEntity user = ( UserEntity ) RedisUtils.get ( userEntity.getToken () );
        return ResultBody.success ( user );
    }


}
